﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 系统参数 添加 修改 
//最后修改: 2010-10-11

using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Collections;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Param_save : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_SysParam;
        }
        #endregion

        #region 页面变量
        protected bool m_IsAdd = true;
        protected int m_cid = 0;
        protected string  m_VarKey = "";
        ParamTypeManage m_ParamTypeManage = new ParamTypeManage();
        protected string m_Title = "系统设置";
        protected string m_strRefer = "admin_setting.aspx?cid={0}";
        #endregion

        #region 页面事件

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadBase();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMSG = "参数名 、关键字或参数值";
            if (!PageAssist.CheckForm(txtVarName, ref strMSG) ||
                !PageAssist.CheckForm(txtVarKey, ref strMSG) ||
                !PageAssist.CheckForm(txtVarValue, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            int.TryParse(GetRequestViewState("cid"), out m_cid);
            ParamTypeInfo oParamTypeInfo = m_ParamTypeManage.GetInfoById(m_cid);
            if (oParamTypeInfo != null)
            {
                ArrayList ArrayValue = new ArrayList();
                m_VarKey = GetRequestViewState("VarKey").Trim();
                if (m_VarKey.IsEmpty()) //如果为空，则表示添加，此时判断是否参数关键词重复
                {
                    ArrayValue = SYSConfig.GetVarValue(txtVarKey.Text.Trim(), oParamTypeInfo.KeyWord);
                    if (ArrayValue != null)
                    {
                        Alert("操作出错，要添加的参数“" + txtVarKey.Text.Trim() + "”已经存在。");
                        return;
                    }
                    else
                    {
                        ArrayValue = new ArrayList();
                    }
                }
                ArrayValue.Add(txtVarKey.Text);
                ArrayValue.Add(txtVarName.Text);
                ArrayValue.Add(txtVarValue.Text);
                ArrayValue.Add(rbtValType.SelectedValue.Trim());
                ArrayValue.Add(rbtDelType.SelectedValue.Trim());
                if (m_cid > 0)
                    m_strRefer = string.Format(m_strRefer, m_cid);
                else
                    m_strRefer = string.Format(m_strRefer, 1);
                base.SaveLog(Plugin.LogKeys.OperObject.TemplateOper, Plugin.LogKeys.OperResult.EditSubmit);
                SubmitAlertAndThisNewLocation(SYSConfig.SaveVarValue(Config.VarAray.GetVarAttribute(), ArrayValue, oParamTypeInfo.KeyWord), "admin_setting.aspx?cid=" + m_cid);
            }
        }

        #endregion

        #region 页面方法

        public void LoadBase()
        {

            int.TryParse(  InitRequestViewState("cid"), out m_cid);
            if (m_cid > 0)
                m_strRefer = string.Format(m_strRefer, m_cid);
            else
                m_strRefer = string.Format(m_strRefer, 1);
            ParamTypeInfo oParamTypeInfo = m_ParamTypeManage.GetInfoById(m_cid);
            rbtValType.DataSource = PageAssist.GetAllVarInputType();
            rbtValType.DataTextField = "Key";
            rbtValType.DataValueField = "Value";
            rbtValType.DataBind();
            rbtValType.SelectedIndex = 0;
            if (oParamTypeInfo != null)
            {
                m_Title = oParamTypeInfo.Name;
                m_VarKey = InitRequestViewState("VarKey").Trim();
                ViewState["VarKey"] = m_VarKey = Microsoft.JScript.GlobalObject.unescape(m_VarKey);
                if (!m_VarKey .Equals(string.Empty))
                {
                    ArrayList oVarArray = SYSConfig.GetVarValue(m_VarKey, oParamTypeInfo.KeyWord);
                    if (oVarArray != null)
                    {
                        txtVarKey.Text = oVarArray[0].ToString();
                        txtVarName.Text = oVarArray[1].ToString();
                        txtVarValue.Text = oVarArray[2].ToString();
                        rbtValType.SelectedValue = oVarArray[3].ToString();
                        rbtDelType.SelectedValue = oVarArray[4].ToString();
                        txtVarKey.Enabled = false;
                        m_IsAdd = false;
                    }
                    else
                    {
                        Alert("操作出错，要修改的参数不存在");
                        btnSubmit.Enabled = false;//按钮不可用
                    }
                }
            }
            else
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
        }
        #endregion
    }
}
